package com.free.semantic.core.businessfunction.impl;

import com.free.semantic.common.RangeObj;
import com.free.semantic.core.ConceptManage;
import com.free.semantic.core.Context;
import com.free.semantic.core.businessfunction.BasicFunction;
import com.free.semantic.core.businessfunction.ObjectConst;
import com.free.semantic.core.script.impl.ScriptEngineImpl;
import com.free.semantic.facade.Link;
import com.free.semantic.facade.Node;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class ScriptToNet implements BasicFunction {

    ScriptEngineImpl semanticNetEngine = new ScriptEngineImpl();

    @Override
    public List<RangeObj> require() {
        return Lists.newArrayList(RangeObj.one(ObjectConst.SCRIPT));
    }

    @Override
    public RangeObj provider() {
        return RangeObj.one(ObjectConst.NET_STRUCT);
    }

    @Override
    public void calculate(Context context) {
        List<String> listValue = context.getListValue(ObjectConst.SCRIPT);
        String script = listValue.get(0);
        ConceptManage conceptManage = semanticNetEngine.buildConceptFromScript(script);
        List<Link> allLink = conceptManage.getAllLink();
        List<Node> allNode = conceptManage.getAllNode();
        Map<String,Object> netInfo =new HashMap<>();
        netInfo.put("links",allLink);
        netInfo.put("nodes",allNode);
        context.setValue(ObjectConst.NET_STRUCT,netInfo);

    }
}